<!doctype html>
<html>
<head>
	<meta charset="UTF-8">
	<title></title>
	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
	<link href="../css/mui.min.css" rel="stylesheet" />
	<link rel="stylesheet" href="../css/setting.css" />
</head>
<body class="gray">
	<div class="mui-content">
	    <!--<ul class="mui-table-view">
            <li class="mui-table-view-cell link" _id="account" _title="帐号与安全" _href="account.html">
                <a class="mui-navigate-right">
                    帐号与安全
                </a>
            </li>
        </ul>-->
        <ul class="mui-table-view " style="margin-top: 0;">
            <li class="mui-table-view-cell link" _id="rephone" _title="更换手机" _href="rephone.html">
                <a class="mui-navigate-right">
                		<span class="mui-pull-right" id="mobile"></span>
                    电话号码
                </a>
            </li>
            <li class="mui-table-view-cell link" _id="forget" _title="找回密码" _href="forget.html" >
                <a class="mui-navigate-right">
                    修改密码
                </a>
            </li>
        </ul>
        <ul class="mui-table-view" style="margin-top: 10px;">
            <li class="mui-table-view-cell"  >
                 微信
                 <div class="mui-switch mui-switch-mini " id="weixin">
					<div class="mui-switch-handle"></div>
				</div>
            </li>
            <!--<li class="mui-table-view-cell android" >
                新浪微博
                <div class="mui-switch mui-switch-mini ">
					<div class="mui-switch-handle"></div>
				</div>
            </li>-->
        </ul>
        <ul class="mui-table-view " style="margin-top: 10px;">
            <li class="mui-table-view-cell ova clearcache android">
            		<span class="mui-pull-right red">66.36M</span>
                    清除缓存
            </li>
            <li class="mui-table-view-cell ova">
            		<span class="mui-pull-right" id="vo"></span>
                    版本号
            </li>
        </ul>
        
        <ul class="mui-table-view singout" style="margin-top: 10px;">
            <li class="mui-table-view-cell singoutli" >
                    退出登录
            </li>
        </ul>
        <!--_id="login" _href="login.html" _aniShow="zoom-fade-out"-->
	</div>
<script src="../js/mui.js"></script>
<script src="../js/bexta.js"></script>
<script type="text/javascript">
	mui.init({
		swipeBack:true
	});
	function chunkCache(clearcache){
		plus.cache.calculate(function(size) {
			var cache = (size/1024/1024).toFixed(2) == 0  ? 0 : (size/1024/1024).toFixed(2);
			clearcache.querySelector('span').innerHTML = (size/1024/1024).toFixed(2)  + 'M';
		});
	}
	mui.plusReady(function () {
		var clearcache = document.querySelector('.clearcache');
		var users = bexta.getStorage(USER);
		document.querySelector('#mobile').innerHTML = users.mobile;
		chunkCache(clearcache)
		document.querySelector('#vo').innerHTML = "v" + plus.runtime.version;
		clearcache.addEventListener('tap', function(){
			plus.cache.clear(function(){
				mui.toast("清除成功");
				chunkCache(clearcache);
			});
		});
		//退出
		document.querySelector('.singoutli').addEventListener('tap', function(){
			bexta.rmStorage(USER);
			bexta.jsonToString(bexta.getStorage(USER));
			bexta.goWebview(function(){
				mui.fire(plus.webview.getLaunchWebview(), 'toggleClick', {
					path:'index2'
				});
			});
		});
		console.log( bexta.jsonToString(users) );
		if( users.wx_id == 1 ){
			document.querySelector('#weixin').classList.add('mui-active');
		}
//		document.querySelector("#weixin").addEventListener('tap', function(){
//			return false;
//		})
		//第三方登录
		var oauth = {};
		plus.oauth.getServices(function(res){
			for( var i in res ){
				if( res[i].id == 'weixin' ){
					oauth[res[i].id] = res[i];
				}
			}
		});
		function getUserInfor(o, r){
			bexta.ajax(api.third_login, function(res){
				mui.toast('绑定成功');
				bexta.setStorage(USER, res);
				ouser.logout(function(){});
			},{data:{uid:users.uid, type:o.id, third_key:o.userInfo.openid}});
		}
		document.querySelector("#weixin").addEventListener("toggle",function(event){
			var that = this;
			if(event.detail.isActive){
			   if( users.wx_id == 1 ){
			   	return ;
			   }
			   mui.confirm("您确定要绑定微信?", '绑定', CONFIRM, function(item){
			   		if( item.index == 1 ){
			   			var o = oauth[that.id];
			   			if(o){
			   				o.login(function(result){
			   					o.getUserInfo(function(){
			   						getUserInfor(o, result);
			   					});
								
							}, function(){
								mui.toast('登录失败');
							});
			   			}
			   		}
			   });
			}else{
				mui.confirm('您确定要解绑微信?','解绑', CONFIRM, function(item){
					if(item.index == 1){
						bexta.ajax(api.third_login, function(res){
							if( !res ){
								mui.toast("解绑成功");
								users.wx_id = 0;
								bexta.setStorage(USER, users);
							}
							
						}, {data:{uid:users.uid, type:'weixin'}}, function(){
							mui("#weixin").switch().toggle();
						});
					}else{
						mui("#weixin").switch().toggle();
					}
				});
			}
		})
		
		
		
	});
</script>
</body>
</html>